package cn.gz.demo.utils.glide;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;

import cn.gz.demo.R;

/**
 * Created by Liuyz on 2017/9/25.
 */
public class GlideUtil {
    /**
     * 没缓存
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadPicNoCash(Context context, String url, ImageView view) {
        //22222
        Glide.with(context).load(url).apply(getOptions(0)).into(view);
    }

    public static void loadPicNoCash(Context context, int resourceId, ImageView view) {
        Glide.with(context).load(resourceId).apply(getOptions(1)).into(view);

    }

    public static void loadPicNoCash(Context context, Uri uri, ImageView view) {
        Glide.with(context).load(uri).apply(getOptions(1)).into(view);
    }

    /**
     * 有缓存
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadPicWithCash(Context context, String url, ImageView view) {
        Glide.with(context).load(url).apply(getOptions(1)).into(view);
    }

    public static void loadPicWithCash(Context context, int resourceId, ImageView view) {
        Glide.with(context).load(resourceId).apply(getOptions(1)).into(view);
    }

    public static void loadPicWithCash(Context context, Uri uri, ImageView view) {
        Glide.with(context).load(uri).apply(getOptions(1)).into(view);
    }

    /**
     * 不带默认图
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadPicNoDefault(Context context, String url, ImageView view) {
        Glide.with(context).load(url).apply(getOptions(2)).into(view);
    }

    public static void loadPicNoDefault(Context context, int resourceId, ImageView view) {
        Glide.with(context).load(resourceId).apply(getOptions(2)).into(view);
    }

    /**
     * 带默认图
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadPicWithDefault(Context context, String url, ImageView view) {
        Glide.with(context).load(url).apply(getOptions(5)).into(view);
    }

    public static void loadPicWithDefault(Context context, int resourceId, ImageView view) {
        Glide.with(context).load(resourceId).apply(getOptions(5)).into(view);
    }

    public static void loadPicWithDefault(Context context, Uri uri, ImageView view) {
        Glide.with(context).load(uri).apply(getOptions(5)).into(view);
    }

    public static void loadPicWithDefault(Context context, String url, int resourcesId, ImageView view) {
        Glide.with(context).load(url).apply(getOptions(5, resourcesId)).into(view);
    }

    public static void loadPicWithDefault(Context context, int resourceId, int resourcesId, ImageView view) {
        Glide.with(context).load(resourceId).apply(getOptions(5, resourcesId)).into(view);
    }

    public static void loadPicWithDefault(Context context, Uri uri, int resourcesId, ImageView view) {
        Glide.with(context).load(uri).apply(getOptions(5, resourcesId)).into(view);
    }

    public static void loadPicNoDefault2(Context context, String url, int resourcesId, ImageView view) {
        Glide.with(context).load(url).apply(getOptions(6, resourcesId)).into(view);
    }

    /**
     * 圆形裁剪
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadCropCirclePic(Context context, String url, ImageView view) {

        RequestOptions options = new RequestOptions()
                .fitCenter()
//                .placeholder(R.drawable.default_icon)
//                .error(R.drawable.default_icon)
                .transform(new CropCircleTransformation(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(view);

    }

    /**
     * 圆形裁剪
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadCropCirclePic(Context context, String url, int resoureId, ImageView view) {

        RequestOptions options = new RequestOptions()
                .fitCenter()
                .placeholder(resoureId)
                .error(resoureId)
                .transform(new CropCircleTransformation(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(view);

    }


    /**
     * 圆角处理
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadRoundedCornersPic(Context context, String url, ImageView view) {
        RequestOptions options = new RequestOptions()
//                .centerCrop()
//                .placeholder(R.drawable.default_icon)
                .error(R.mipmap.error_icon)
                .transform(new GlideRoundTransform(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(view);
    }

    public static void loadRoundedCornersPic(Context context, String url, int radius, ImageView view) {
        RequestOptions options = new RequestOptions()
//                .centerCrop()
//                .placeholder(R.drawable.default_icon)
                .error(R.mipmap.error_icon)
                .transform(new GlideRoundTransform(context, radius))
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(view);
    }

    /**
     * 灰度处理
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadGrayscaleCornersPic(Context context, String url, ImageView view) {
        RequestOptions options = new RequestOptions()
                .fitCenter()
//                .placeholder(R.drawable.default_icon)
                .error(R.mipmap.error_icon)
                .transform(new GrayscaleTransformation(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(view);
    }

    /**
     * 模糊处理
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadBlurTransPic(Context context, String url, ImageView view) {
        RequestOptions options = new RequestOptions()
                .fitCenter()
//                .placeholder(R.drawable.default_icon)
                .error(R.mipmap.error_icon)
                .transform(new BlurTransformation(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(view);
    }

    /**
     * 渐变处理
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadShadeTransPic(Context context, String url, ImageView view) {
        RequestOptions options = new RequestOptions()
                .fitCenter()
//                .placeholder(R.drawable.default_icon)
                .error(R.mipmap.error_icon)
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context)
                .load(url)
                .apply(options)
                .transition(new DrawableTransitionOptions().crossFade(2000))
                .into(view);
    }


    public static RequestOptions getOptions(int type) {
        RequestOptions options = null;
        if (type == 0) { // 设置没默认
            options = new RequestOptions()
                    .centerCrop()
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.NONE);
        } else if (type == 1) { // 设置有缓存
            options = new RequestOptions()
                    .centerCrop()
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        } else if (type == 2) { // 设置没缓存有失败图
            options = new RequestOptions()
                    .centerCrop()
//                    .error()
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.NONE);
        } else if (type == 3) { //设置有缓存有失败图
            options = new RequestOptions()
                    .centerCrop()
                    .error(R.mipmap.error_icon)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        } else if (type == 4) { // 设置没缓存有失败图有占位图
            options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.error_icon)
                    .error(R.mipmap.error_icon)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.NONE);
        } else if (type == 5) { // 设置有缓存有失败图有占位图
            options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.error_icon)
                    .error(R.mipmap.error_icon)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        }
        return options;
    }

    public static RequestOptions getOptions(int type, int resourcesId) {
        RequestOptions options = null;
        if (type == 0) { // 设置没默认
            options = new RequestOptions()
                    .centerCrop()
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.NONE);
        } else if (type == 1) { // 设置有缓存
            options = new RequestOptions()
                    .centerCrop()
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        } else if (type == 2) { // 设置没缓存有失败图
            options = new RequestOptions()
                    .centerCrop()
                    .error(resourcesId)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.NONE);
        } else if (type == 3) { //设置有缓存有失败图
            options = new RequestOptions()
                    .centerCrop()
                    .error(resourcesId)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        } else if (type == 4) { // 设置没缓存有失败图有占位图
            options = new RequestOptions()
                    .centerCrop()
                    .placeholder(resourcesId)
                    .error(resourcesId)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.NONE);
        } else if (type == 5) { // 设置有缓存有失败图有占位图
            options = new RequestOptions()
                    .centerCrop()
                    .placeholder(resourcesId)
                    .error(resourcesId)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        } else if (type == 6) { // 设置有缓存有失败图有占位图
            options = new RequestOptions()
                    .placeholder(resourcesId)
                    .error(resourcesId)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        }
        return options;
    }

    public static void loadCertificationPic(Context context, String url, int resourseId, ImageView view) {
        RequestOptions options = new RequestOptions()
                .placeholder(resourseId)
                .error(resourseId)
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(view);
    }

    public static void loadCertificationPic(Context context, String url, ImageView view) {
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(view);
    }
}
